//
//  VVSportControlViewController.m
//  DingDingSport
//
//  Created by 王惠平 on 2017/4/21.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVSportControlViewController.h"
#import "VVSportTrackController.h"
#import "UIColor+CZAddition.h"
#import "VVSportCameraConteoller.h"
#import <MediaPlayer/MediaPlayer.h>

@interface VVSportControlViewController ()<MPMediaPickerControllerDelegate>

//轨迹控制器
@property (nonatomic,strong) VVSportTrackController *trackVC;

//轨迹按钮 用于获取中心点
@property (weak, nonatomic) IBOutlet UIButton *trackBtn;

//GPS状态
@property (weak, nonatomic) IBOutlet UIButton *gpsBtn;

//距离
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

//时长
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

//平均速度
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;

//继续按钮x方向的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueXConstraint;
//结束按钮x方向的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishXConstraint;

//暂停按钮
@property (weak, nonatomic) IBOutlet UIButton *paushBtn;

@property (weak, nonatomic) IBOutlet UIButton *continueBtn;

@property (weak, nonatomic) IBOutlet UIButton *finishBtn;

//记录是否正在执行运动状态的操作
@property (nonatomic,assign) BOOL isAnimating;

@end

@implementation VVSportControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   //创建控制器
    self.trackVC = [[VVSportTrackController alloc]init];
    
    //传递轨迹模型
    self.trackVC.track = self.track;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote:) name:@"VVSportLocationDidUpdateNote" object:nil];
    
}

- (void)locationDidUpdateNote:(NSNotification *)note {
    
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f",self.track.totalDistance];
    self.timeLabel.text = self.track.timeStr;
    
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.2f",self.track.avgSpeed];
    
    if (!self.isAnimating) {
        
        //获取当前的位置对象
        CLLocation *location = note.userInfo[@"VVSportLocationDidUpdateNoteLocationKey"];
        
        //判断速度
        if (location.speed == 0 && self.track.sportType == VVSportStateContinue) {
            
            [self clickSportStateBtn:self.paushBtn];
            
        } else if (location.speed > 0 && self.track.sportType == VVSportStatePause) {
            
            [self clickSportStateBtn:self.continueBtn];
            
        }
        
    }
  
}

//响应事件
//点击播放音乐
- (IBAction)clickSettingBtn:(id)sender {
    
    //打开媒体库
    MPMediaPickerController *pickerVc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    
    //设置属性
    pickerVc.delegate = self;
    
    //modal展示
    [self presentViewController:pickerVc animated:YES completion:nil];

}

//代理方法实现
//选中某个曲目后调用
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    
    //创建播放器
    MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
    
    //设置播放序列
    [player setQueueWithItemCollection:mediaItemCollection];
    
    //执行播放
    [player play];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
//点击取消后调用
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击相机按钮
- (IBAction)clickCameraBtn:(id)sender {
    
    VVSportCameraConteoller *cameraVC = [[VVSportCameraConteoller alloc]init];
    
    //传递轨迹模型
    cameraVC.track = self.track;

    [self presentViewController:cameraVC animated:YES completion:nil];

   
}


- (IBAction)clickSportStateBtn:(UIButton *)sender {
    
    //用户开始操作后,停止自动处理
    self.isAnimating = YES;
    
    //改变运动状态
    self.track.sportState = sender.tag;
    
    switch (sender.tag) {
        case VVSportStatePause:
        {
            //继续按钮左移
            self.continueXConstraint.constant -= 90;
            
            //结束按钮右移
            self.finishXConstraint.constant += 90;
            
            //暂停按钮隐藏
            self.paushBtn.hidden = YES;
            
            //在到达指定位置前,禁用继续&停止
            self.continueBtn.enabled = NO;
            self.finishBtn.enabled = NO;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                
                //用户操作的动画执行完,恢复自动处理
                self.isAnimating = NO;
                
                self.continueBtn.enabled = YES;
                self.finishBtn.enabled = YES;
            }];
            
        }
            
            break;
            
        case VVSportStateContinue:
        
        {
            //继续按钮右偏移
            self.continueXConstraint.constant += 90;
            
            //结束按钮左移
            self.finishXConstraint.constant -= 90;
            
            //在到达指定位置前,禁用继续&停止
            self.continueBtn.enabled = NO;
            self.finishBtn.enabled = NO;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                [self.view layoutIfNeeded];
                
            } completion:^(BOOL finished) {
                
                //暂停按钮隐藏
                self.paushBtn.hidden = NO;
                
                //用户操作的动画执行完,恢复自动处理
                self.isAnimating = NO;
                
                self.continueBtn.enabled = YES;
                self.finishBtn.enabled = YES;

            }];
            
        }
            
            break;
            
        default:
            break;
    }
    
}

- (IBAction)clickTrackBtn:(id)sender {
    
    [self presentViewController:self.trackVC animated:YES completion:nil];
    
}

//添加背景图层
- (void)addBackgroundLayer {
    
    //创建渐变图层
    CAGradientLayer *layer = [CAGradientLayer layer];
    
    //设置尺寸
    layer.bounds = self.view.bounds;
    
    //设置位置
    layer.position = self.view.center;
    
    //获取16进制颜色
    CGColorRef color1 = [UIColor cz_colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor cz_colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor cz_colorWithHex:0x406578].CGColor;
    
    //添加颜色数组
    layer.colors = @[(__bridge id)color1,(__bridge id)color2,(__bridge id)color3];
    
    //设置位置
    layer.locations = @[@0,@(0.6),@1];
    
    //添加图层
    [self.view.layer insertSublayer:layer atIndex:0];
}

- (void)viewDidLayoutSubviews {
    
    [super viewDidLayoutSubviews];
    
    //传递轨迹按钮中心点
    self.trackVC.trackBtnCenter = self.trackBtn.center;
    
    //添加背景图层
    [self addBackgroundLayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
